package yx.action;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;

import yx.service.FamilyServiceInt;
import yx.vo.FamilyVO;
import yx.vo.JsonVO;

import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("basePackage")
@Namespace("/")
@Action(value = "familyAction")
public class FamilyAction extends BaseAction implements ModelDriven<FamilyVO> {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(FamilyAction.class);

	private FamilyVO family = new FamilyVO();

	@Override
	public FamilyVO getModel() {
		return family;
	}

	private FamilyServiceInt familyService;

	public FamilyServiceInt getFamilyService() {
		return familyService;
	}

	@Autowired
	public void setFamilyService(FamilyServiceInt familyService) {
		this.familyService = familyService;
	}

	public void queryFamily() {
		/*
		 * TODO：需要调查为什么加判定
		 */
		if (family.getStudentId() != null) {
			super.writeJson(familyService.datagrid(family));
		}
	}

	public void addFamily() {
		JsonVO j = new JsonVO();
		try {
			FamilyVO u = familyService.save(family);
			j.setSuccess(true);
			j.setMsg("增加成功！");
			j.setObj(u);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		super.writeJson(j);
	}

	public void removeFamily() {
		familyService.remove(family.getIds());
		JsonVO j = new JsonVO();
		j.setSuccess(true);
		j.setMsg("删除成功！");
		super.writeJson(j);
	}

	public void editFamily() {
		FamilyVO u = familyService.edit(family);
		JsonVO j = new JsonVO();
		j.setSuccess(true);
		j.setMsg("编辑成功！");
		j.setObj(u);
		super.writeJson(j);
	}

}
